import os


def open_r():
    """
    读取文件
    :return:
    """
    file = open('file2.txt', mode='r', encoding='utf-8')
    text = file.read()
    print(text)
    file.close()


def open_rw():
    """
    读写取文件
    :return:
    """
    file = open('file2.txt', mode='r+', encoding='utf-8')
    text = file.read()
    print(text)
    file.write('world')
    file.close()


def copy_file():
    file1 = open('img.png', mode='rb+')
    file2 = open('img_copy.png', mode='wb')
    b = file1.read()
    file2.write(b)
    file1.close()
    file2.close()


def chage_dir():
    """
    改变路径
    :return:
    """
    print(os.getcwd())
    os.chdir('dir2')
    print(os.getcwd())


def scan_dir(current_path, width):
    file_list = os.listdir(current_path)  # 得到当前路径下的所有文件
    for file in file_list:
        print(' ' * width, file)  # 打印文件名，width代表有多少个空格
        new_path = current_path + '/' + file  # 把当前路径和文件名拼接到一起
        if os.path.isdir(new_path):
            scan_dir(new_path, width + 4)


if __name__ == '__main__':
    # open_r()
    # change_dir()
    scan_dir('E:\\python code 2025\\2', 0)
